using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;

namespace MyPhotoIndex
{
    public partial class ProgressForm : Form
    {
        private List<Control> m_tempControls = new List<Control>();
        private Object m_syncLock = new object();

        public ProgressForm()
        {
            InitializeComponent();

            this.VisibleChanged+=new EventHandler(ProgressForm_VisibleChanged);
            this.ClientSizeChanged += new EventHandler(ProgressForm_ClientSizeChanged);

            timer1.Tick += new EventHandler(timer1_Tick);
        }

        void ProgressForm_ClientSizeChanged(object sender, EventArgs e)
        {
            this.panel1.Height = this.Height - 2;
            this.panel1.Refresh();
        }

        void ProgressForm_VisibleChanged(object sender, EventArgs e)
        {
            lock (m_syncLock)
            {
                timer1.Enabled = Visible;
            }
        }

        public override void Refresh()
        {
            base.Refresh();
            this.Height = (ActiveProcesses.Count * 20) + 10;

            LoadList();
        }

        private void LoadList()
        {
            tableLayoutPanel1.RowCount = ActiveProcesses.Count;
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 20));
            //tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 250f));
            //tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100f));
            //tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 50f));

            foreach (Control control in m_tempControls)
            {
                tableLayoutPanel1.Controls.Remove(control);
                control.Dispose();
            }

            m_tempControls.Clear();

            int row = 0;
            foreach (ActiveProcess process in ActiveProcesses.Instance)
            {
                ColorProgressBar newProgress = new ColorProgressBar();
                newProgress.Height = 15;
                newProgress.Width = 100;
                newProgress.Office2007Style = true;
                newProgress.Parent = tableLayoutPanel1;
                newProgress.Visible = true;
                newProgress.Tag = process;
                m_tempControls.Add(newProgress);

                Label descriptionLabel = new Label();

                descriptionLabel.Font = new Font("verdana", 6f, FontStyle.Bold);
                descriptionLabel.TextAlign = ContentAlignment.MiddleLeft;
                descriptionLabel.Dock = DockStyle.Fill;
                descriptionLabel.Parent = tableLayoutPanel1;
                descriptionLabel.Visible = true;
                descriptionLabel.Tag = process;
                m_tempControls.Add(descriptionLabel);

                Label timeLeftLabel = new Label();

                timeLeftLabel.Font = new Font("verdana", 6f, FontStyle.Bold);
                timeLeftLabel.TextAlign = ContentAlignment.MiddleLeft;
                timeLeftLabel.Dock = DockStyle.Fill;
                timeLeftLabel.Parent = tableLayoutPanel1;
                timeLeftLabel.Visible = true;
                timeLeftLabel.Tag = process;
                timeLeftLabel.Name = "timeLeft";
                m_tempControls.Add(timeLeftLabel);

                tableLayoutPanel1.SetCellPosition(descriptionLabel, new TableLayoutPanelCellPosition(0, row));
                tableLayoutPanel1.SetCellPosition(newProgress, new TableLayoutPanelCellPosition(1, row));
                tableLayoutPanel1.SetCellPosition(timeLeftLabel, new TableLayoutPanelCellPosition(2, row++));
            }
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            lock (m_syncLock)
            {
                foreach (Control control in m_tempControls)
                {
                    ActiveProcess process = control.Tag as ActiveProcess;
                    if (control is ColorProgressBar)
                    {
                        ColorProgressBar cpb = control as ColorProgressBar;
                        cpb.Maximum = process.Maximum;
                        cpb.Value = process.Value;
                        cpb.Style = (process.IsInfinite == true) ? ProgressBarStyle.Marquee : ProgressBarStyle.Continuous;
                    }
                    else
                    if (control is Label)
                    {
                        Label label = control as Label;
                        if (label.Name.CompareTo("timeLeft") == 0)
                        {
                            label.Text = process.Stats.EstimatedTimeLeft.ToString();
                        }
                        else
                        {
                            label.Text = process.ProcessDescription;
                        }
                    }
                }
            }
        }
    }
}